package online.flowerinsnow.minecraft.bukkit.testffa.gui;

import online.flowerinsnow.minecraft.bukkit.testffa.Main;
import online.flowerinsnow.minecraft.bukkit.testffa.manager.PlayerKitManager;
import online.flowerinsnow.minecraft.bukkit.testffa.util.ColorUtils;
import online.flowerinsnow.minecraft.bukkit.testffa.util.ItemStackFactory;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;

public class KitEditGUI {
    public final Inventory inventory;
    public final Player player;

    public KitEditGUI(@NotNull Player player) {
        inventory = Bukkit.createInventory(null, 45, "编辑Kit");
        this.player = player;

        init();
    }

    private void init() {
        ConfigurationSection config = PlayerKitManager.getPlayerKit(player);
        if (config == null) {
            player.kickPlayer("§c您的数据加载错误 若该问题持续出现 请联系管理员");
            return;
        }
        for (int i = 0; i < 9; i++) {
            inventory.setItem(i + 27, config.getItemStack(Integer.toString(i)));
        }
        for (int i = 9; i < 18; i++) {
            inventory.setItem(i + 9, config.getItemStack(Integer.toString(i)));
        }
        for (int i = 18; i < 27; i++) {
            inventory.setItem(i - 9, config.getItemStack(Integer.toString(i)));
        }
        for (int i = 27; i < 36; i++) {
            inventory.setItem(i - 27, config.getItemStack(Integer.toString(i)));
        }
        inventory.setItem(36, new ItemStackFactory(Material.DIAMOND_HELMET)
                .addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2, true)
                .unbreakable()
                .toItemStack());
        inventory.setItem(37, new ItemStackFactory(Material.DIAMOND_CHESTPLATE)
                .addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2, true)
                .unbreakable()
                .toItemStack());
        inventory.setItem(38, new ItemStackFactory(Material.DIAMOND_LEGGINGS)
                .addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2, true)
                .unbreakable()
                .toItemStack());
        inventory.setItem(39, new ItemStackFactory(Material.DIAMOND_BOOTS)
                .addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2, true)
                .unbreakable()
                .toItemStack());
        inventory.setItem(43, new ItemStackFactory(Material.EMERALD_BLOCK)
                .setDisplayName("&a保存", true)
                .toItemStack());
        inventory.setItem(44, new ItemStackFactory(Material.REDSTONE_BLOCK)
                .setDisplayName("&c放弃", true)
                .toItemStack());
    }

    public void open() {
        if (!player.isOnline()) return;
        player.openInventory(inventory);
        Bukkit.getPluginManager().registerEvents(new Listener(), Main.getInstance());
    }

    private class Listener implements org.bukkit.event.Listener {
        @EventHandler
        public void onClick(InventoryClickEvent e) {
            if (!e.getWhoClicked().equals(player)) {
                return;
            }
            if (!e.getClickedInventory().equals(inventory)) {
                e.setCancelled(true);
                return;
            }
            if (e.getSlot() > 35) {
                e.setCancelled(true);
                if (e.getSlot() == 43) { // 点击了确定按钮
                    YamlConfiguration config = new YamlConfiguration();
                    for (int i = 0; i < 9; i++) {
                        config.set(Integer.toString(i), inventory.getItem(i + 27));
                    }
                    for (int i = 9; i < 18; i++) {
                        config.set(Integer.toString(i), inventory.getItem(i + 9));
                    }
                    for (int i = 18; i < 27; i++) {
                        config.set(Integer.toString(i), inventory.getItem(i - 9));
                    }
                    for (int i = 27; i < 36; i++) {
                        config.set(Integer.toString(i), inventory.getItem(i - 27));
                    }
                    player.closeInventory();
                    PlayerKitManager.savePlayerKit(player, config);
                    player.playSound(player.getLocation(), Sound.CHICKEN_EGG_POP, 1.0F, 1.0F);
                } else if (e.getSlot() == 44) {
                    player.closeInventory();
                    player.playSound(player.getLocation(), Sound.NOTE_BASS, 1.0F, 1.0F);
                }
            }
        }

        @EventHandler
        public void onDrag(InventoryDragEvent e) {
            if (e.getWhoClicked().equals(player)) {
                e.setCancelled(true);
            }
        }

        @EventHandler
        public void onDrop(PlayerDropItemEvent e) {
            if (e.getPlayer().equals(player)) {
                e.getItemDrop().remove();
                e.getPlayer().closeInventory();
                e.getPlayer().sendMessage(ColorUtils.parseColor("&7[&c×&7]&c请勿丢弃物品"));
            }
        }

        @EventHandler
        public void onClose(InventoryCloseEvent e) {
            if (e.getPlayer().equals(player)) {
                HandlerList.unregisterAll(this);
            }
        }
    }
}
